//
//  CotTypeSelectController.m
//  Tazama NGO
//
//  Created by Day, Patrick B. on 2/29/12.
//  Copyright (c) 2012 The MITRE Corporation. All rights reserved.
//

#import "CotTypeSelectController.h"
#import "Settings.h"
#import "CoTTypes.h"
@implementation CotTypeSelectController

@synthesize saveButton,cancelButton,cotTypes,tableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Do any additional setup after loading the view from its nib.
    
    CoTTypes *cts = [[CoTTypes alloc] init];
    self.cotTypes = [cts getAllCoTTypes];
    
    //[self.tableView setDataSource:cotTypes];
}
  
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return cotTypes.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero] autorelease];
    } 
    // Set up the cell...
    CoTType *cellValue = [cotTypes objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@\t(%@)", [cellValue unitType], [cellValue unitAbbreviation]];
    
    return cell;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    currSelection = [cotTypes objectAtIndex:indexPath.row];
    
    //Store in settings
    if (currSelection) {
        NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
        [settings setObject:currSelection.cotType forKey:currCoTType];
        //Save settings
        [settings synchronize];
    }
    
    [[self navigationController] popViewControllerAnimated:YES];
    
    [self dismissModalViewControllerAnimated:YES];
}
-(IBAction)saveButtonClick:(id)sender {
    
    //Store in settings
    if (currSelection) {
    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    [settings setObject:currSelection.cotType forKey:currCoTType];
    //Save settings
    [settings synchronize];
    }
    
    [[self navigationController] popViewControllerAnimated:YES];
    
    [self dismissModalViewControllerAnimated:YES];
    
}
-(IBAction)cancelButtonClick:(id)sender {
    //Discard changes, go back to map view
    [self dismissModalViewControllerAnimated:YES];
}

@end
